package jrain.fw.business.common;

import java.util.function.Consumer;

import jrain.fw.core.business.BusinessRequest;
import jrain.fw.core.business.BusinessResponse;
import jrain.fw.core.business.client.BusinessApiClient;
import jrain.fw.core.service.Service;
import jrain.fw.core.service.ServiceConst;
import jrain.fw.core.utils.FwUtils;

@Service(group = ServiceConst.S_BUSINESS_API_CLIENT, module = ServiceConst.M_BUSINESS)
public class BusinessApiClientService implements BusinessApiClient {

	// private static final Logger log = LoggerFactory.getLogger("business-api-client");

	@Override
	public BusinessResponse doSync(BusinessRequest request) {
		return doSync(request, null);
	}

	@Override
	public BusinessResponse doSync(BusinessRequest request, Consumer<BusinessRequest> consumer) {
		if (consumer != null) {
			consumer.accept(request);
		}
		BusinessResponse response = FwUtils.getBusinessInnerClient().doSync(request);
		return response;
	}

	@Override
	public void doAsync(BusinessRequest request, Consumer<BusinessResponse> callbak) {
		doAsync(request, null, callbak);
	}

	@Override
	public void doAsync(BusinessRequest request, Consumer<BusinessRequest> consumer, Consumer<BusinessResponse> callbak) {
		if (consumer != null) {
			consumer.accept(request);
		}

		FwUtils.getBusinessInnerClient().doAsync(request, callbak);
	}

	@Override
	public String doSyncStr(BusinessRequest request) {
		return doSyncStr(request, null);
	}

	@Override
	public String doSyncStr(BusinessRequest request, Consumer<BusinessRequest> consumer) {
		BusinessResponse response = doSync(request, consumer);
		return FwUtils.getJsonService().toPageJson(response);
	}

}
